/*
 * @Description: ------------ fileDescription -----------
 * @Author: snows_l snows_l@163.com
 * @Date: 2024-12-22 15:53:03
 * @LastEditors: snows_l snows_l@163.com
 * @LastEditTime: 2024-12-24 09:30:19
 * @FilePath: \Backstage\Server\src\router\config.js
 */
const express = require('express');
const db = require('../utils/connDB');
const { verifyToken } = require('../utils/handleToken');
const { handleError } = require('../utils/common.js');

const router = express.Router();

// 获取blog配置信息
router.get('/blog/info', (req, res) => {
  try {
    let sql = `SELECT * FROM project_config WHERE id = 'blog'`;
    db.queryAsync(sql)
      .then(result => {
        res.send({
          code: 200,
          data: result.results[0],
          msg: 'success'
        });
      })
      .catch(err => {
        handleError(res, err);
      });
  } catch (error) {
    handleError(res, error);
  }
});

// 更新blog配置信息
router.post('/blog/update', (req, res) => {
  let token = req.cookies['bs-cookie'];
  try {
    const user = verifyToken(token);
    if (user.role_key !== 'admin') throw new Error('ROLE_ERROR:权限不足');
    let paramKeys = Object.keys(req.body);
    let paramValues = Object.values(req.body);
    let sql = `UPDATE project_config SET `;
    for (let i = 0; i < paramKeys.length; i++) {
      if (i === 0) {
        sql += `${paramKeys[i]} = ? `;
      } else {
        sql += `, ${paramKeys[i]} = ? `;
      }
    }
    sql += `WHERE id = 'blog'`;
    let params = paramValues;
    db.queryAsync(sql, params)
      .then(result => {
        res.send({
          code: 200,
          data: result,
          msg: 'success'
        });
      })
      .catch(err => {
        handleError(res, err);
      });
  } catch (error) {
    handleError(res, error);
  }
});

// 获取bs配置信息
router.get('/bs/info', (req, res) => {
  try {
    let sql = `SELECT * FROM project_config WHERE id = 'bs'`;
    db.queryAsync(sql)
      .then(result => {
        res.send({
          code: 200,
          data: result[0],
          msg: 'success'
        });
      })
      .catch(err => {
        handleError(res, err);
      });
  } catch (error) {
    handleError(res, error);
  }
});

// 更新bs配置信息
router.post('/bs/update', (req, res) => {
  let token = req.cookies['bs-cookie'];
  try {
    const user = verifyToken(token);
    if (user.role_key !== 'admin') throw new Error('ROLE_ERROR:权限不足');
    let paramKeys = Object.keys(req.body);
    let paramValues = Object.values(req.body);
    let sql = `UPDATE project_config SET `;
    for (let i = 0; i < paramKeys.length; i++) {
      if (i === 0) {
        sql += `${paramKeys[i]} = ? `;
      } else {
        sql += `, ${paramKeys[i]} = ? `;
      }
    }
    sql += `WHERE id = 'bs'`;
    let params = paramValues;
    db.queryAsync(sql, params)
      .then(result => {
        res.send({
          code: 200,
          data: result,
          msg: 'success'
        });
      })
      .catch(err => {
        handleError(res, err);
      });
  } catch (error) {
    handleError(res, error);
  }
});

module.exports = router;
